class CatalogueController < ApplicationController
  before_filter :initialize_cart
  LIST_SIZE = 5
  PAGE_SIZE = 4 # max results per page (on index)

  def index
    @page_title = Artwork.l_name_pl.capitalize

    @artworks = Artwork.find(:all, :limit => PAGE_SIZE)
    @artworks_count = Artwork.count(:conditions => 'published = true')
  end
  
  # Ajaxing more search results on index
  def ajax_results
    @page_to_load = params[:page].to_i
    @artworks = Artwork.find(:all, :limit => PAGE_SIZE, :offset => @page_to_load * PAGE_SIZE)
  end

  def show
    @artwork = Artwork.find(params[:id])
    @page_title = "Viewing \"" + @artwork.name + "\" by " + @artwork.artist.name + " from the " + @artwork.artist.region.name + " region"

    @otherByArtist = Array.new
    temp = @artwork.artist.artworks
    
    for t in temp
      if t != @artwork
        @otherByArtist.push(t)
      end
    end
    
    @otherByArtist = @otherByArtist.sort_by { rand }[0..(LIST_SIZE-1)]
    
    @similarArt = Array.new
    
    for category in @artwork.categories
      for a in category.artworks
        if a != @artwork
          @similarArt.push(t)
        end
      end
    end
    
    @similarArt = @similarArt.uniq
    @similarArt = @similarArt.sort { rand(3)-1 }[0..(LIST_SIZE-1)]
    
    @desc_languages = @artwork.artwork_descriptions
    
  end

  def search
  end

  def rss
    @articles = Artwork.find(:all, :order => "id DESC")
    render :layout => false
    headers["Content-Type"] = "application/xml; charset=utf-8"
  end
end
